package com.dev.web.controller.safe;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dev.bbs.domain.BbsArticle;
import com.dev.bbs.domain.BbsUser;
import com.dev.bbs.service.IBbsArticleService;
import com.dev.common.annotation.Log;
import com.dev.common.constant.ShiroConstants;
import com.dev.common.core.controller.BaseController;
import com.dev.common.core.domain.AjaxResult;
import com.dev.common.enums.BusinessType;
import com.dev.framework.util.ShiroUtils;

/**
 * 社区文章Controller
 *
 * @author ruoyi
 * @date 2019-09-17
 */
@Controller
@RequestMapping("safe/article")
public class SafeArticleController extends BaseController
{
    private String             prefix = "article";

    @Autowired
    private IBbsArticleService bbsArticleService;

    /**
    * 新增社区文章
    */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存社区文章
     */
    @Log(title = "社区文章", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BbsArticle bbsArticle)
    {
        BbsUser bbsUser = ShiroUtils.getBbsUser();
        bbsArticle.setAuthor(bbsUser.getUserId()).setCreateBy(bbsUser.getNickname());
        bbsArticle.setBbsUser(bbsUser);
        return toAjax(bbsArticleService.insertBbsArticle(bbsArticle)).put("artId", bbsArticle.getId());
    }

    /**
     * 修改社区文章
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BbsArticle bbsArticle = bbsArticleService.selectBbsArticleById(id);
        mmap.put("bbsArticle", bbsArticle);
        return prefix + "/edit";
    }

    /**
     * 修改保存社区文章
     */
    @Log(title = "社区文章", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BbsArticle bbsArticle)
    {
        return toAjax(bbsArticleService.updateBbsArticle(bbsArticle));
    }

    /**
     * 设置社区文章状态、制定、加精
     */
    @RequiresRoles(value = {ShiroConstants.BBS_ADMIN})
    @Log(title = "社区文章", businessType = BusinessType.UPDATE)
    @PostMapping("set")
    @ResponseBody
    public AjaxResult set(Long id, String field, Boolean rank)
    {
        BbsArticle bbsArticle = new BbsArticle().setId(id);
        if ("rec".equals(field))
        {
            bbsArticle.setRec(rank);
        }
        else if ("top".equals(field))
        {
            bbsArticle.setTop(rank);
        }
        return toAjax(bbsArticleService.updateBbsArticle(bbsArticle));
    }

    /**
    * 删除社区文章
    */
    @Log(title = "社区文章", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long id)
    {
        BbsUser bbsUser = ShiroUtils.getBbsUser();
        return toAjax(bbsArticleService.deleteBbsArticle(id, bbsUser));
    }
}
